package cn.biplam.back.operator.ddl.sql2md;

import java.util.ArrayList;

import cn.biplam.back.sql.dml.Delete;
import cn.biplam.back.sql.dml.Insert;
import cn.biplam.back.sql.dml.Update;
import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.Operator;
import cn.biplam.common.client.OperatorResult;
import cn.biplam.common.interfaces.DataOperator;
import cn.biplam.common.opcode.sqlengine.DDLParserCode.MdImpTskCode;
import cn.biplam.common.struc.MdStruc;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.sqlresult.SqlResult;

/**
 * SQL2MD任务管理表数据操作类<br>
 * <li> 插入批次信息
 * <li> 获取批次信息
 * 
 * @author XiaoRuxing 2009-6-30上午09:41:26
 * @version 1.0
 */
public class ModelImportTask extends DataOperator{

	private static final String Table_Name = "MODEL_IMPORT_TASK";
	
	public ModelImportTask() throws ErrorMessage{
		super(Table_Name);
	}
	
	@Override
	protected OperatorResult _exchange() throws ErrorMessage {
		ArrayList<String> sql = new ArrayList<String>();
		if(this.opId.equals(MdImpTskCode.Insert)){
			this.insert(sql, filter);
			return this.doing(sql, Operator_Type.DML);
		}else if(this.opId.equals(MdImpTskCode.Update)){
			this.update(sql, filter);
			return this.doing(sql, Operator_Type.DML);
		}else if(this.opId.equals(MdImpTskCode.Get_All_Task)){
			this.getTask(sql, filter);
			return this.doing(sql, Operator_Type.Select);
		}else if(this.opId.equals(MdImpTskCode.Get_Task_By_ID)){
			this.getTaskById(sql, filter);
			return this.doing(sql, Operator_Type.Select);
		}else if(this.opId.equals(MdImpTskCode.Delete_By_ID)){
			this.deleteByID(sql, filter);
			return this.doing(sql, Operator_Type.DML);
		}else{
			throw new ErrorMessage("Data Operator id not found:"+ opId+" !");
		}
	}	

	/**
	 * 创建新任务 
	 * @param sql
	 */
	public void insert(ArrayList<String> sql,DataFilter filter) throws ErrorMessage {
		String id=generatorRowID();
		Insert insert=new Insert(this.tableName);
		insert.putStringFieldDenyNull(MdStruc.MdImpTaskStruc.TASK_ID, id);
		insert.putStringFieldIgnoreNull(MdStruc.MdImpTaskStruc.TASK_DESC, filter.getValue(MdStruc.MdImpTaskStruc.TASK_DESC));
		insert.putStringFieldIgnoreNull(MdStruc.MdImpTaskStruc.MODEL_TYPE, filter.getValue(MdStruc.MdImpTaskStruc.MODEL_TYPE));
		insert.putStringFieldIgnoreNull(MdStruc.MdImpTaskStruc.MODEL_THEME, filter.getValue(MdStruc.MdImpTaskStruc.MODEL_THEME));
		insert.putStringFieldIgnoreNull(MdStruc.MdImpTaskStruc.STATUS, filter.getValue(MdStruc.MdImpTaskStruc.STATUS));
		insert.putStringFieldIgnoreNull(MdStruc.MdImpTaskStruc.START_TIME, filter.getValue(MdStruc.MdImpTaskStruc.START_TIME));
		insert.putStringFieldIgnoreNull(MdStruc.MdImpTaskStruc.END_TIME, filter.getValue(MdStruc.MdImpTaskStruc.END_TIME));
		insert.putStringFieldIgnoreNull(MdStruc.MdImpTaskStruc.DEFAULT_SUB_THEME, filter.getValue(MdStruc.MdImpTaskStruc.DEFAULT_SUB_THEME));
		sql.add(insert.getSimpleSql());
	}
	
	/**
	 * 更新任务状态  
	 */
	public void update(ArrayList<String> sql,DataFilter filter) throws ErrorMessage{
		Update update=new Update(this.tableName);
		update.putStringFieldIgnoreNull(MdStruc.MdImpTaskStruc.STATUS, filter.getValue(MdStruc.MdImpTaskStruc.STATUS));
		update.putStringFieldIgnoreNull(MdStruc.MdImpTaskStruc.START_TIME, filter.getValue(MdStruc.MdImpTaskStruc.START_TIME));
		update.putStringFieldIgnoreNull(MdStruc.MdImpTaskStruc.END_TIME, filter.getValue(MdStruc.MdImpTaskStruc.END_TIME));
		update.setWhere(MdStruc.MdImpTaskStruc.TASK_ID+"='"+filter.getValue(MdStruc.MdImpTaskStruc.TASK_ID)+"'");
		sql.add(update.getSimpleSql());
	}
	
	/**
	 * 查询所有任务 
	 * @param sql
	 * @param filter
	 */
	public void getTask(ArrayList<String> sql,DataFilter filter){
		StringBuilder sp = new StringBuilder();
		sp.append(" select * from  "+this.tableName);
		sp.append("  order by start_time desc");
		sql.add(sp.toString());
	}
	
	/**
	 * 获取单个任务
	 * @param sql
	 * @param filter
	 */
	public void getTaskById(ArrayList<String> sql,DataFilter filter){
		StringBuilder sp = new StringBuilder();
		sp.append(" select * from "+this.tableName);
		sp.append(" where "+MdStruc.MdImpTaskStruc.TASK_ID+"='"+filter.getValue(MdStruc.MdImpTaskStruc.TASK_ID)+"'");
		sql.add(sp.toString());
	}
	
	/**
	 * 更新任务状态  
	 */
	public void deleteByID(ArrayList<String> sql,DataFilter filter) throws ErrorMessage{
		String taskId = filter.getValue(MdStruc.MdImpTaskStruc.TASK_ID);
		/** 删除buffer*/
		String modelType = null;
		SqlResult sr = Operator.get(MdImpTskCode.Get_Task_By_ID, userId, filter).getSelectResult();
		if(TOOLS.exist(sr)){
			modelType = sr.get(0, MdStruc.MdImpTaskStruc.MODEL_TYPE);
		}
		DataFilter buffer = new DataFilter();
		buffer.put(MdStruc.SrcTabBufferStruc.BATCH, taskId);
		buffer.put(MdStruc.SrcTabBufferStruc.MODEL_TYPE, modelType);
		new TableBuffer().deleteByTaskID(sql, buffer);
		/** 删除比较表*/
		DataFilter cmfilter = new DataFilter();
		cmfilter.put(MdStruc.ImpCompTabStruc.TASK_ID, taskId);
		new OptCompareTable()._deleteByTaskID(sql, cmfilter);
		/** 删除任务sql*/
		Delete delete=new Delete(this.tableName);
		delete.setWhere(MdStruc.MdImpTaskStruc.TASK_ID+"='"+taskId+"'");
		sql.add(delete.getSimpleSql());
	}
}
